{extend name="public/common"}

{block name="style"}
<title>党群建设发布</title>
<link rel="stylesheet" href="/home/css/construction/send.css?v={$Think.config.version}">
{/block}

{block name="body"}
<div id="app" v-cloak>
    <div class="text">
        <div class="text-box">
            <textarea v-model="text" placeholder="请输入内容..." oninput="if(value.length>400)value=value.slice(0,400);"></textarea>
        </div>
    </div>
    <div class="picture">
        <div class="img-box">
            <div class="img" v-for="(imgUrl,index) of img">
                <img @click="delImg(index)" class="close" src="/home/images/common/close.png"/>
                <img :src="imgUrl" @click="loadImg(imgUrl,img)"/>
            </div>
            <div class="img add" @click="imgUpload" v-if="img.length < 3">
                <img src="/home/images/common/add.png"/>
            </div>
        </div>
        <p>字数限400，图片限3张（可不传）</i></p>
        <form ref="myform">
            <input type="file" accept="image/jpg, image/png, image/gif, image/jpeg" @change="handleFileChange"
                   ref="upimg" style="display: none;">
        </form>
    </div>
    <div class="foot">
        <p>注：点击发布后将发布到随手拍中，同事将保存到个人中心我的发布中</p>
        <div class="send" @click="send">发布</div>
    </div>
</div>
{/block}

{block name="script"}
<script src="https://res.wx.qq.com/open/js/jweixin-1.2.0.js"></script>
<script>
    var vw = new Vue({
        el: "#app",
        data: {
            text: '',
            img: [],
            images: [],
            type : 1
        },
        created() {
            // this.pushHistory();
            this.type = parseInt(window.sessionStorage.getItem('construction_type'));
            console.log(this.type)
            wx.config({
                debug: false,
                appId: {$jsSign}.appid,
                timestamp:{$jsSign}.timestamp ,
                nonceStr: {$jsSign}.noncestr,
                signature: {$jsSign}.signature,
                jsApiList: ['previewImage']
            });
        },
        methods: {
            pushHistory() {
                window.addEventListener("popstate", function (e) {
                    self.location.reload();
                }, false);
                var state = {
                    title: "",
                    url: "#"
                };
                window.history.replaceState(state, "", "#");
            },
            delImg(e) {
                this.img.splice(e, 1);
                this.images.splice(e, 1);
                this.$refs.myform.reset(); //表单重置，解决input onchange选重问题
            },
            imgUpload() {
                var upImg = this.$refs.upimg;
                upImg.click();
            },
            handleFileChange(e) {
                var _this = this;
                var upImg = _this.$refs.upimg;
                var img = upImg.files[0];
                if (img) {
                    var newSize = (img.size / 1024).toFixed(2);
                    if (newSize <= 10240) {
                        _this.$indicator.open({
                            text: '图片上传中',
                            spinnerType: 'triple-bounce'
                        });
                        var formData = new FormData();
                        formData.append('picture', img);
                        _this.$http.post("{:Url('File/uploadPicture')}", formData).then(function (res) {
                            var data = JSON.parse(res.data);
                            console.log(data)
                            if (data.code == 1) {
                                // 在获取到文件对象进行预览
                                _this.$indicator.close();
                                _this.img.push(data.data.path); //上传后图片路径
                                _this.images.push(parseInt(data.data.id));
                                this.$refs.myform.reset(); //表单重置，解决input onchange选重问题
                            } else {
                                _this.$indicator.close();
                                _this.$toast({
                                    message: "上传失败，请重新上传！",
                                    duration: 1500
                                });
                            }
                        }, function (res) {
                            _this.$indicator.close();
                            _this.$toast({
                                message: "上传失败，请重新上传！",
                                duration: 1500
                            });
                        });
                    } else {
                        _this.$toast("单张上传图片大小不能超过10M,请重新上传！");
                    }
                }
            },
            //            微信jssdk - 图片预览
            loadImg(src, imglist) {
                var urls = [];
                var current = window.location.protocol + '//' + window.location.host + src;
                for (var i in imglist) {
                    urls[i] = window.location.protocol + '//' + window.location.host + imglist[i];
                }
                wx.previewImage({
                    current: current,
                    urls: urls
                });
            },
            send() {
                var _this = this;
                if (!_this.text) {
                    _this.$toast({
                        message: "请输入发布的内容",
                        duration: 1500
                    });
                    return false;
                }
                _this.$indicator.open({
                    text: '正在发布中',
                    spinnerType: 'triple-bounce'
                });
                _this.$http.post("{:Url('Construction/send')}", {
                    'type': _this.type,
                    'content': _this.text,
                    'images': _this.images
                }, {emulateJSON: true}).then(function (res) {
                    console.log(res.data)
                    var data = res.data;
                    if (data.code == 1) {
                        _this.$indicator.close();
                        _this.$toast({
                            message: "发布成功，返回中",
                            iconClass: 'icon',
                            duration: 1500
                        });
                        setTimeout(function () {
                            window.history.back();
                        },1000);
                    } else {
                        _this.$indicator.close();
                        _this.$toast({
                            message: "发布失败，请重试！",
                            duration: 1500
                        });
                    }
                }, function (res) {
                    _this.$indicator.close();
                    _this.$toast({
                        message: "发布失败，请重试！",
                        duration: 1500
                    });
                });
            }
        }
    })
</script>
{/block}
